/*
 * =====================================================================================
 *
 *       Filename:  gendict.cpp
 *
 *    Description:  
 *
 *        Version:  1.0
 *        Created:  2015/05/29 20时46分40秒
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  Liu Yi (), swliuyi@gmail.com
 *   Organization:  
 *
 * =====================================================================================
 */
#include <fstream>
#include <iostream>
#include <string>
#include <cstring>
#include <cstdlib>
#include "hmm.h"
#include "dictmatch.h"
using namespace std;

void det(Hmm & hmm, Dict & dic, string entity, vector<pair<unsigned long, unsigned long> > * wordList, size_t &start)
{
    if( start >= wordList->size() )
    {
        return;
    }
    string word = "";
    while( start < wordList->size() && entity == hmm.getStr((*wordList)[start].second).substr(0, entity.size()) )
    {
        word += hmm.getStr((*wordList)[start].first);
        word += " ";
        start++;
    }
    if( word.size() )
    {
        word = word.substr(0, word.size()-1);
        if( word.size() < 4)
        {
            return;
        }
        dic.Add(word);
    }
}


int main(int argc, char* argv[])
{
    Hmm hmm;
    if (argc<3) {
        cerr << "USAGE: ./this input output" << endl;
        return 1;
    }
    ifstream istrm(argv[1]);
    string name = argv[2];

    name += ".name";
    string loc = argv[2];
    loc += ".loc";
    string org = argv[2];
    org += ".org";

    vector<vector<pair<unsigned long, unsigned long> > *> trainingSequences;
    hmm.preProcess(istrm, trainingSequences, 1);

    ofstream fileName(name.c_str());
    ofstream fileLoc(loc.c_str());
    ofstream fileOrg(org.c_str());
    Dict nameDict;
    Dict locDict;
    Dict orgDict;

    for( size_t i = 0; i < trainingSequences.size() ; i++ )
    {
        size_t j = 0;
        while(j < trainingSequences[i]->size())
        {
            if(hmm.getStr((*trainingSequences[i])[j].second) == "o")
            {
                j++;
                continue;
            }
            det(hmm, nameDict, "nr", trainingSequences[i], j);
            det(hmm, locDict, "ns", trainingSequences[i], j);
            det(hmm, orgDict, "nt", trainingSequences[i], j);
        }
    }
    nameDict.Print(fileName);
    locDict.Print(fileLoc);
    orgDict.Print(fileOrg);
}
